package com.example.demo.controller;



import com.example.demo.configuration.config.MqttGateway;
import jakarta.annotation.Resource;
import lombok.AllArgsConstructor;
import org.springframework.integration.core.MessageProducer;
import org.springframework.integration.mqtt.inbound.MqttPahoMessageDrivenChannelAdapter;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@AllArgsConstructor
@RestController
@RequestMapping("/test")
public class MqttTestController {

    @Resource
    private MqttGateway mqttGateway;

    private MqttPahoMessageDrivenChannelAdapter messageProducer;


    /**
     * sendData 消息
     * topic 订阅主题
     * **/
    @RequestMapping("/sendMqtt1")
    public String sendMqtt(String sendData, String topic){


        mqttGateway.sendToMqtt(topic, sendData);
        return "OK";
    }

    /**
     * sendData 消息
     * qos 消息级别 （对应QOS0、QOS1，QOS2）
     * topic 订阅主题
     * **/
    @RequestMapping("/sendMqtt2")
    public String sendMqtt(String  sendData, int qos, String topic){


        mqttGateway.sendToMqtt(topic, qos, sendData);
        return "OK";
    }



    @RequestMapping("/addMqttTopic")
    public String addMqttTopic(@RequestParam("topic") String topic){
        messageProducer.addTopic(topic, 0);
        return "OK";
    }
}